package leetcode;


//岛屿最大面积
public class LC695 {

    int[] dx = {0,0,-1,1};
    int[] dy = {-1,1,0,0};

    int dfs(int[][] grid, int x, int y) {
        int cnt = 1;
        grid[x][y] = 0;
        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (nx >= 0 && nx < grid.length && ny >=0 && ny < grid[0].length &&
                grid[nx][ny] == 1) {
                cnt += dfs(grid, nx, ny);
            }
        }
        return cnt;
    }

    public int maxAreaOfIsland(int[][] grid) {

        int ans = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == 1) {
                    ans = Math.max(ans,dfs(grid,i,j));
                }
            }
        }
        return ans;
    }
}
